package com.qidian.buy.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qidian.buy.entity.ContentCategory;
import com.qidian.buy.content.mapper.ContentCategoryMapper;
import com.qidian.buy.content.service.IContentCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qidian.buy.util.StatusCode;
import com.qidian.buy.util.pojo.BuyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 内容分类 服务实现类
 * </p>
 *
 * @author 戴着假发的程序员
 * @since 2023-05-18
 */
@Service
public class ContentCategoryServiceImpl extends ServiceImpl<ContentCategoryMapper, ContentCategory> implements IContentCategoryService {

    @Autowired
    private ContentCategoryMapper contentCategoryMapper;
    @Override
    public BuyResult<ContentCategory> queryByParentId(Long parentId) {
        QueryWrapper<ContentCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        wrapper.ne("status",StatusCode.STATUS_DELETE);
        return BuyResult.ok(contentCategoryMapper.selectList(wrapper));
    }

    @Override
    public BuyResult<ContentCategory> saveOrUpdateContentCat(ContentCategory cat) {
        // 检擦id是否存在
        ContentCategory contentCategory = contentCategoryMapper.selectById(cat.getId());
        if(contentCategory != null){
            //修改
            cat.setUpdated(new Date());
            cat.setIsParent(null);
            contentCategoryMapper.updateById(cat);
        }else{
            // 新增
            // 补全数据
            cat.setId(null);
            cat.setUpdated(new Date());
            cat.setCreated(new Date());
            cat.setSortOrder(1);
            cat.setStatus(1);
            cat.setIsParent(false);
            // 保存
            if(contentCategoryMapper.insert(cat)==1) {
                // 新增完成之后，要判断父节点的isParent是否是1，如果不是1，就要将父节点的isParent修改为1
                ContentCategory parentCat = contentCategoryMapper.selectById(cat.getParentId());
                if(!parentCat.getIsParent()){// 原来不是父节点
                    parentCat.setIsParent(true);
                    // 修改父节点
                    contentCategoryMapper.updateById(parentCat);
                }
            }
        }
        return BuyResult.ok(cat);// 将保存或者修改后的cat对象包裹到结果中返回
    }

    @Override
    public BuyResult deleteContentCat(Long id) {
        // 不要做物理删除，修改状态即可
        ContentCategory cat = new ContentCategory();
        cat.setId(id);
        cat.setStatus(StatusCode.STATUS_DELETE);
        if(contentCategoryMapper.updateById(cat)==1){
            //删除完成之后要判断当前元素的父节点是否还有其它子节点，如果没有就需要将父节点的isParent修改为false
            ContentCategory currCat = contentCategoryMapper.selectById(id);
            // 查询父节点
            ContentCategory parentCat = contentCategoryMapper.selectById(currCat.getParentId());
            if(parentCat != null ){
                // 查询这个父节点是否还有子节点
                QueryWrapper<ContentCategory> wrapper = new QueryWrapper<>();
                wrapper.eq("parent_id",parentCat.getId());
                List<ContentCategory> children = contentCategoryMapper.selectList(wrapper);
                if(children == null || children.size() == 0){
                    // 没有子节点, 修改父节点的isParent为false
                    parentCat.setIsParent(false);
                    contentCategoryMapper.updateById(parentCat);
                }
            }
        }
        return BuyResult.ok();
    }
}
